package com.itheima.web.controller;

import com.itheima.domain.store.Syslog;
import com.itheima.domain.system.User;
import com.itheima.service.store.OrdersService;
import com.itheima.service.store.ProductService;
import com.itheima.service.store.SyslogService;
import com.itheima.service.store.impl.OrdersServiceImpl;
import com.itheima.service.store.impl.ProductServiceImpl;
import com.itheima.service.store.impl.SyslogServiceImpl;

import com.itheima.service.system.PermissionService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.UserService;
import com.itheima.service.system.impl.PermissionServiceImpl;
import com.itheima.service.system.impl.RoleServiceImpl;
import com.itheima.service.system.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;


public class BaseServlet extends HttpServlet {

    protected OrdersService ordersService;
    protected ProductService productService;
    protected UserService userService;
    protected SyslogService syslogService;
    protected PermissionService permissionService;
    protected RoleService roleService;


    public void init(){
        ordersService=new OrdersServiceImpl();
        productService=new ProductServiceImpl();
        userService=new UserServiceImpl();
        syslogService=new SyslogServiceImpl();
        permissionService =new PermissionServiceImpl();
        roleService =new RoleServiceImpl();


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        try {
            /*User user = (User) request.getSession().getAttribute("user");
            String username = user.getUsername();
            String ip = request.getRemoteAddr();
            StringBuffer requestURL = request.getRequestURL();
            String string = requestURL.toString();*/
            long start = System.currentTimeMillis();
            Syslog syslog =new Syslog();
            syslog.setVisitTimeStr(new Date().toLocaleString());
            User user = (User) request.getSession().getAttribute("user");
            if (user!=null){
                syslog.setUsername(user.getUsername());
            }else{
                syslog.setUsername("firstTime");
            }
            syslog.setIp(request.getRemoteAddr());
            syslog.setUrl(request.getRequestURI());



            String methodName = request.getParameter("operation");
            Class clazz = this.getClass();
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);

            long end = System.currentTimeMillis();
            syslog.setExecutionTime(end-start);

           if(!"list".equals(methodName)){
                syslog.setMethod(methodName);
                syslogService.save(syslog);
         }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
